#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#include "w_rizhi.h"
#include "lianjie.h"
//#include "gongneng.h"

enum ml{
    ls=1,
    quit,
    get,
    put,
    cd,
    erro,
};
typedef struct File {
    enum ml s;
    char name[128];
    char md5[60];
    long len;
    char sub[5000];
} file;
enum ml get_enum(char *s){
    if(strncmp(s,"ls",2)==0){
        return ls;
    }else if(strncmp(s,"quit",4)==0){
        return quit;
    }else if(strncmp(s,"get",3)==0){
        return get;
    }else if(strncmp(s,"put",3)==0){
        return put;
    }else if(strncmp(s,"cd",2)==0){
        return cd;
    }else{
        return erro;
    }
}
void fasong(int fd,file ff){
      strtok(ff.name,"-");
      char *p=strtok(NULL,"-");
      strcpy(ff.name,p);
      FILE *fp=fopen(ff.name,"r");
      fread(ff.sub,sizeof(ff.sub),1,fp);
     fclose(fp);
      printf("%s\n",ff.sub);
     write(fd,&ff,sizeof(ff));
 }
  void huoqu(int fd,file ff)
 {
     read(fd,&ff,sizeof(ff));
      FILE *fp=fopen(ff.name,"w+");
     fwrite(ff.sub,sizeof(ff.sub),1,fp);
      fclose(fp);
      printf("%s\n",ff.sub);
  }

int main(int argv, char **argc) {
    //连接服务端
     int sockfd=getsock(argc[1],argc[2]);
     char mm[8];
     while(1){
         printf("请输入密码：");
         gets(mm);
         write(sockfd,mm,sizeof(mm));
         read(sockfd,mm,sizeof(mm));
         if(strcmp(mm,"yyyyyyy")==0){
            break;
         }
     }
    //功能实现
    file ff;
    
    char s[5]={0};
    while (1) {
        printf(">");
        gets(ff.name);
        ff.s=get_enum(ff.name);
        switch(ff.s){
            case 1:
                write(sockfd,&ff,sizeof(ff));
                read(sockfd,&ff,sizeof(ff));
                printf("%s",ff.sub);
                break;
            case 2:
                write(sockfd,&ff,sizeof(ff));
                close(sockfd);
                return 0;
                break; 
            case 3:
                write(sockfd,&ff,sizeof(ff));
                huoqu(sockfd,ff);
                break; 
            case 4:
                write(sockfd,&ff,sizeof(ff));
                fasong(sockfd,ff);
                break; 
            case 5:
                write(sockfd,&ff,sizeof(ff));
                break;
            default:
                printf("无效命令\n");
                break;
        }
        printf("-----------------\n");
        memset(&ff,0,sizeof(ff));
    }
    /*  file ff;
      ff.sub=NULL;

      printf("请输入要传输文件的路径名:\n");
      scanf("%s",ff.name);
      //打开这个文件，只读模式打开
      FILE *stream=fopen(ff.name,"r");
      if(stream==NULL){
          perror("fopen");
          exit;
      }
      //获取文件长度
      fseek(stream, 0,SEEK_END );
      ff.len= ftell(stream);
      fseek(stream, 0,SEEK_SET);
      //给文件正文分配内存
      ff.sub=(char *)malloc(sizeof(char)*ff.len);
      //将分配的内存空间清0
      memset(ff.sub,0,sizeof(ff.sub));
      //获取文件正文
      size_t a=fread(ff.sub,ff.len, 1,stream);
      w_rizhi("name:%slen:%dgetlen:%ld\nsub:%s\n",ff.name,ff.len,a,ff.sub);
      fclose(stream);
     // close(sockfd);
  */
    return 0;
}
